<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Media extends Controller
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //一级分类
        $subscr = Db::name('column')
            ->where('pid = 0')
            ->select();


        //热门
        $hots = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->select();

        //未登录栏目
        $hotss = Db::name('column')
            ->where('pid > 0 and hot = 1')
            ->limit(9)
            ->select();
        //未登入下栏目
        $hotssz = Db::name('column')
            ->where('pid > 0 and hot = 1')
            ->limit(10)
            ->select();

        //判断是否有订阅
        if (session('uid', '', 'think')) {
            $list = Db::name('subscribe s')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->field('cid')
                ->select();
            $list2 = array();
            foreach ($list as $key => $v) {
                $list2[] = $v['cid'];
            }

            $list3 = null;
            foreach ($list2 as $key => $value) {
                $list3 .= $value . ',';
            }
        } else {
            $list3 = null;
        }

        if (session('uid', '', 'think')) {
            $ass = Db::name('subscribe s')
                ->join('column c', 's.cid = c.cid')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->order('id desc')
                ->limit(10)
                ->select();

        } else {
            $ass = null;
        }

        //登录栏目条
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        // var_dump($assz);die;
        return view('Media/yidianhao', [
            'subscr' => $subscr,
            'hotss' => $hotss,
            'hotssz' => $hotssz,
            'hots' => $hots,
            'list3' => $list3,
            'ass' => $ass,
            'assz' => $assz,
        ]);
    }


    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {

        //一级分类
        $subscr = Db::name('column')
            ->where('pid = 0')
            ->select();

<<<<<<< HEAD

=======
<<<<<<< HEAD
       
>>>>>>> 2d79cfa8789da2f2a423d006d49f501a7ccfc9c7
            $hots = Db::name('column')
                ->where('pid =' . $id)
                ->select();
=======

        //判断是否为热门
            $hots = Db::name('column')
                ->where('pid =' . $id)
                ->select();

>>>>>>> eb37ef826ab0653d162d4a05c8238d99ea85e487

        //判断是否有订阅
        if (session('uid', '', 'think')) {
            $list = Db::name('subscribe s')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->field('cid')
                ->select();

            $list2 = array();
            foreach ($list as $key => $v) {
                $list2[] = $v['cid'];
            }

            $list3 = null;
            foreach ($list2 as $key => $value) {
                $list3 .= $value . ',';
            }
        } else {
            $list3 = null;
        }

        if (session('uid', '', 'think')) {
            $ass = Db::name('subscribe s')
                ->join('column c', 's.cid = c.cid')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->order('id desc')
                ->limit(10)
                ->select();


        } else {
            $ass = null;
        }
        //登录栏目条
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        //未登入下栏目
        $hotssz = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->select();
        //未登录栏目
        $hotss = Db::name('column')
            ->where('pid > 0 and hot = 1')
            ->select();

        return view('Media/yidianhao', [
            'subscr' => $subscr,
            'hots' => $hots,
            'hotss' => $hotss,
            'hotssz' => $hotssz,
            'list3' => $list3,
            'ass' => $ass,
            'assz' => $assz,
        ]);

    }


    //订阅
    public function sub($cid)
    {
        // return $cid;
        // $list = Db::name('subscribe s')
        // ->join('user_self u','u.uid = s.uid')
        // ->join('column c','c.cid = s.cid')
        // ->field('c.cid')
        // ->select();
        $data = ['uid' => session('uid', '', 'think'), 'cid' => $cid];
        $list = DB::name('subscribe')->insert($data);
        return json($list);
    }

    //取消订阅
    public function cancel($cid)
    {
        $uid = session('uid', '', 'think');
        $data = [$uid, $cid];
        // $list = DB::name('subscribe')->where('uid','cid',$data)->delete();
        $list = Db::query("delete from subscribe where uid = $uid and cid = $cid");
        return json($list);
    }

}
